# -*- coding:utf-8 -*-
import json
import sys
import os

import web
from mymodule.config import *

reload(sys)
sys.setdefaultencoding('utf-8')
urls = (
    "/task","task",
    '/tasktest','test',
    '/pause','pause'
)

task_app = web.application(urls, globals())
class task:
    def GET(self):
        web.header('Access-Control-Allow-Origin', '*')
        web.header("Content-Type", "Application/json; charset=utf-8")
        r = redis.Redis(host='127.0.0.1', port=6379, db=0,password=redis_passwd)
        task_data = r.lrange('task',start=0,end=-1)
        task_json = {}
        # return type(task_data)
        # return json.loads(task_data)
        task_json['data']= task_data
        return json.dumps(task_json,ensure_ascii=False)

# 任务暂停
class pause:
    def GET(self):
        web.header('Access-Control-Allow-Origin', '*')
        x = web.input()
        url_domain = x['url_domain']
        r = getrdc()
        taskdata = r.lrange('task', start=0, end=-1)
        r.delete('task')
        for i in taskdata:
            i = eval(i)
            if i['taskName'] == url_domain:
                a = i['pid']
                for t in a:
                    os.system('kill -s 9 ' + t)
                    return t
                i['pid'] = []
                i['taskStatus'] = 1
                i['taskNum'] = 0
            data = json.dumps(i)
            r.lpush('task', data)
        return 666666
class test:
    def GET(self):
        web.header('Access-Control-Allow-Origin', '*')
        web.header("Content-Type", "Application/json; charset=utf-8")
        r = redis.Redis(host='127.0.0.1', port=6379, db=0,password=redis_passwd)
        task_data = r.lrange('task', start=0, end=-1)
        for i in task_data:
            eval(i)
            return i
